package main

import (
	"imooc.com/crawler/engine"
	"imooc.com/crawler/zhenai/parser"
	"imooc.com/crawler/scheduler"
	itemSaverClient "imooc.com/crawler_distrilbuted/persist/client"
	"log"
	workerClient "imooc.com/crawler_distrilbuted/worker/client"
	"net/rpc"
	"imooc.com/crawler_distrilbuted/rpcsupport"
	"flag"
	"strings"
)

var (
	itemSaverHost = flag.String("itemsaver_host", "",
		"itemsaver host")

	workerHosts = flag.String("worker_hosts", "",
		"worker hosts (comma separated)")
)

func main() {
	flag.Parse()

	itemChan, err := itemSaverClient.ItemSaver(*itemSaverHost)
	if err != nil {
		panic(err)
	}
	hosts := strings.Split(*workerHosts, ",")
	pool := createClientPool(hosts)
	processor, err := workerClient.CreateProcessor(pool)
	if err != nil {
		panic(err)
	}
	log.Println("客户端已连接成功....")
	e := engine.ConcurrentEngine{
		Scheduler: &scheduler.QueuedScheduler{},
		WorkerCount: 100,
		ItemChan: itemChan,
		RequestProcessor:processor,
	}
	//e.Run(engine.Request{
	//	Url: 		"https://www.zhenai.com/zhenghun",
	//	ParserFunc: parser.ParseCityList,
	//})
	//测试单个城市
	//e.Run(engine.Request{
	//	Url: "https://www.zhenai.com/zhenghun/shanghai",
	//	ParserFunc: parser.ParseCity,
	//})

	e.Run(engine.Request{
		Url: "https://album.zhenai.com/u/1365187484",
		Parser: parser.NewParseProfile("琪琪"),
	})
}

func createClientPool(hosts []string) chan *rpc.Client {
	var clients []*rpc.Client

	for _,host := range hosts{
		client, err := rpcsupport.NewClient(host)
		if err != nil {
			log.Printf("error connecting to %s: %v", host, err)
			continue
		}
		clients = append(clients, client)
	}

	out := make(chan *rpc.Client)

	go func() {
		for  {
			for _, client := range clients{
				out <- client
			}
		}

	}()

	return out
}

